package com.tanhua.dubbo.server.api;

import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.PageInfo;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.dubbo.server.pojo.Visitors;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: Administrator
 * @date: Created in 2021/2/27 15:37
 * @version: 0.0.1
 * @modified By:
 */
@Service(version = "1.0.0")
public class VisitorsApiImpl implements VisitorsApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public String saveVisitor(Visitors visitors) {
        visitors.setId(ObjectId.get());
        visitors.setDate(System.currentTimeMillis());
        this.mongoTemplate.save(visitors);
        return visitors.getId().toHexString();
    }

    @Override
    public List<Visitors> topVisitor(Long userId, Integer num) {
        Pageable pageable= PageRequest.of(0,num, Sort.by(Sort.Order.desc("date")));
        Query query=new Query(Criteria.where("userId").is(userId)).with(pageable);

        return this.queryVisitorsList(query);
    }

    @Override
    public List<Visitors> topVisitor(Long userId, Long date) {
        Query query=Query.query(Criteria.where("userId").is(userId)
                .and("date").gte(date));
        return this.queryVisitorsList(query);
    }

    @Override
    public PageInfo<Visitors> topVisitor(Long userId, Integer page, Integer pageSize) {
        Pageable pageable=PageRequest.of(page-1,pageSize,Sort.by(Sort.Order.desc("date")));
        Query query=Query.query(Criteria.where("userId").is(userId)).with(pageable);
        List<Visitors> visitors = this.queryVisitorsList(query);
        PageInfo<Visitors>pageInfo=new PageInfo<>();
        pageInfo.setTotal(0);
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        pageInfo.setRecords(visitors);
        return pageInfo;
    }

    private List<Visitors>queryVisitorsList(Query query){
        List<Visitors> visitorsList = this.mongoTemplate.find(query, Visitors.class);
        //查询得分
        for (Visitors visitors : visitorsList) {
            Query queryRecommend=Query.query(Criteria.where("toUserId").is(visitors.getUserId())
            .and("userId").is(visitors.getVisitorUserId()));
            RecommendUser recommendUser = this.mongoTemplate.findOne(queryRecommend, RecommendUser.class);
            if(null!=recommendUser){
                visitors.setScore(recommendUser.getScore());
            }else{
                //设置默认值
                visitors.setScore(30d);
            }
        }
        return visitorsList;
    }
}
